package com.leo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.stage.Stage;

import java.awt.*;
import java.net.URL;

/**
 * @ClassName SystemTrayDemo
 * @Description
 * @Author wangss
 * @date 2020.11.12 17:22
 * @Version 1.0
 */
public class SystemTrayDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        // 剩最后一个窗口时是否关闭程序，true是关闭，false是不关闭
        Platform.setImplicitExit(false);

        AnchorPane pane = new AnchorPane();

        AnchorPane root = new AnchorPane();

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1000);
        primaryStage.setHeight(500);
        primaryStage.centerOnScreen();
        primaryStage.setTitle("系统托盘");
        primaryStage.show();

        // 当退出时需要将模式改变
        primaryStage.setOnCloseRequest(event -> Platform.setImplicitExit(true));

        URL url = this.getClass().getClassLoader().getResource("sound/1.mp3");
        Media media = new Media(url.toExternalForm());
        MediaPlayer mp = new MediaPlayer(media);

        Button play = new Button("播放");
        Button min = new Button("最小化到系统托盘");

        root.getChildren().addAll(play, min);
        AnchorPane.setLeftAnchor(play, 100d);
        AnchorPane.setLeftAnchor(min, 200d);

        play.setOnAction(event -> mp.play());

        min.setOnAction(event -> {
            try {
                SystemTray tray = SystemTray.getSystemTray();
                // 这里图片展示有问题
                URL image1 = this.getClass().getClassLoader().getResource("images/3.png");
                Image image = Toolkit.getDefaultToolkit().getImage(image1);
                String str = "javafx";
                PopupMenu popup = new PopupMenu();
                // 这里如果出现乱码，启动时加上参数 -Dfile.encoding=GBK
                MenuItem menuItem = new MenuItem("显示");
                MenuItem menuItem2 = new MenuItem("exit");
                popup.add(menuItem);
                popup.add(menuItem2);

                TrayIcon ti = new TrayIcon(image, str, popup);
                // 图片全部展示
                ti.setImageAutoSize(true);

                tray.add(ti);

                primaryStage.hide();

                menuItem.setFont(new Font("show", Font.BOLD, 20));
                menuItem2.setFont(new Font("exit", Font.BOLD, 20));

                menuItem.addActionListener(e -> {
                    Platform.runLater(() -> {
                        primaryStage.show();
                    });

                    tray.remove(ti);
                });

                menuItem2.addActionListener(e -> {
                    /*Platform.runLater(()->{
                        primaryStage.close();
                    });*/
                    Platform.exit();
                    tray.remove(ti);
                });

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
